package com.delu.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.delu.config.UserOrderCountVO;
import com.delu.data.Result;
import com.delu.dto.OrderPayDTO;
import com.delu.dto.RenewOrderDTO;
import com.delu.em.ResultEnum;
import com.delu.entity.TravelOrder;
import com.delu.mapper.TravelOrderMapper;
import com.delu.service.TravelOrderService;
import com.delu.utils.IdWorker;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2021-12-18
 */
@Controller
@RequestMapping("/travel-ordre")
@RestController
public class TravelOrderController {

    @Autowired
    private TravelOrderMapper travelOrderMapper;

    @Autowired
    private TravelOrderService travelOrderService;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private RedisTemplate redisTemplate;

    public static final String ORDER_KEY = "order";

    @GetMapping("leaseList")
    @ApiOperation(value = "订单租车类型")
    @ApiImplicitParam(name = "leaseId",value = "基本类型",required = true,paramType = "query")
    public Result leaseList(@RequestParam("leaseId") Integer leaseId){
        List<TravelOrder> travelOrders = travelOrderService.leaseList(leaseId);
        return Result.response(ResultEnum.SUCCESS).setData(travelOrders);

    }

    @GetMapping
    @ApiOperation("根据订单id查询")
    @ApiImplicitParam(name = "orderId",value = "订单ID",required = true,paramType = "query")
    public Result selectPayRank(@RequestParam("orderId")Long orderId){
        System.out.println("订单id" + orderId);
        TravelOrder travelOrder = travelOrderMapper.selectById(orderId);
        return Result.response(ResultEnum.SUCCESS).setData(travelOrder);
    }

    @PutMapping//@RequestParam java中的数据类型如string 加上此注解表名参数为必填项
    //@RequestBody  就收实体类要求前台必须用json的格式传参
    @ApiOperation(value = "订单实体类修改")
    @ApiImplicitParam(name = "travelOrder",value = "订单实体类",required = true,paramType = "path")
    public Result updateTravelOrderById(@RequestBody TravelOrder travelOrder) {
        try {
            travelOrderMapper.updateById(travelOrder);
            return Result.response(ResultEnum.SUCCESS).setData("ok");
        }catch (Exception e){
            e.printStackTrace();
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    @GetMapping("orderList")
    @ApiOperation(value = "查询个人全部订单")
    @ApiImplicitParam(name = "userId",value = "基本类型",required = true,paramType = "query")
    public Result orderList(@RequestParam("userId") Integer userId){
        List<TravelOrder> travelOrders = travelOrderService.orderList(userId);
        return Result.response(ResultEnum.SUCCESS).setData(travelOrders);

    }

    @PostMapping("orderto")
    @ApiOperation(value = "查询个人某一个订单")
    @ApiImplicitParam(name = "travelOrder",value = "订单实体类",required = true,paramType = "body")
    public Result orderto(@RequestBody TravelOrder travelOrder){

        List<TravelOrder> orderto = travelOrderService.orderto(travelOrder);

        return Result.response(ResultEnum.SUCCESS).setData(orderto);
    }

    @GetMapping("refund")
    @ApiOperation(value = "查询退款订单")
    @ApiImplicitParam(name = "userId",value = "用户id",required = true,paramType = "query")
    public Result refund(@RequestParam("userId")Integer userId){
        List<TravelOrder> refund = travelOrderService.refund(userId);
        return Result.response(ResultEnum.SUCCESS).setData(refund);
    }

    @GetMapping("userOrderCount")
    @ApiOperation(value = "查询每个用户的订单数量,无需参数")
    public Result userOrderCount(){
        List<UserOrderCountVO> userOrderCountVOS = travelOrderService.userOrderCount();
        return Result.response(ResultEnum.SUCCESS).setData(userOrderCountVOS);
    }

    @RequestMapping("/renew")
    @ApiOperation(value = "续费修改订单,需要续费到期时间,订单号,保存redis")//续费不需要加分布式锁
    public Result renewPay(String orderId, Date date, BigDecimal price){
        TravelOrder travelOrder = travelOrderMapper.selectById(orderId);
        String  l = String.valueOf(idWorker.nextId());
        OrderPayDTO orderDTO = new OrderPayDTO();
        orderDTO.setOrderId(Long.valueOf(orderId));
        orderDTO.setDate(LocalDate.now());
        orderDTO.setNewOrderId(l);
        orderDTO.setRenewPrice(price);
        orderDTO.setUserId(travelOrder.getUserId());
        //通过续费表来查询
        redisTemplate.boundValueOps(ORDER_KEY + orderDTO.getNewOrderId()).set(orderDTO);
        return Result.response(ResultEnum.SUCCESS).setData(l);
    }


}
